using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsIMPA_REF_INDEX
    {
        public clsIMPA_REF_INDEX(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<IMPA_REF_INDEX> DataTableToLstObj(DataTable dt)
        {
            IMPA_REF_INDEX refIndexDTO;
            List<IMPA_REF_INDEX> lstRefIndexDTO = new List<IMPA_REF_INDEX>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                refIndexDTO = new IMPA_REF_INDEX();
                try
                {
                    refIndexDTO._number = int.Parse(dt.Rows[i]["Number"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    refIndexDTO._l1Code= int.Parse(dt.Rows[i]["L1Code"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    refIndexDTO._jpgName = dt.Rows[i]["JpgName"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    refIndexDTO._description = dt.Rows[i]["Description"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstRefIndexDTO.Add(refIndexDTO);
            }
            return lstRefIndexDTO;
        }
        public IMPA_REF_INDEX DataTableToObj(DataTable dt)
        {
            IMPA_REF_INDEX refIndexDTO = new IMPA_REF_INDEX(); 
            try
            {
                refIndexDTO._number = int.Parse(dt.Rows[0]["Number"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                refIndexDTO._l1Code = int.Parse(dt.Rows[0]["L1Code"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                refIndexDTO._jpgName = dt.Rows[0]["JpgName"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                refIndexDTO._description = dt.Rows[0]["Description"].ToString();
            }
            catch (Exception ex)
            {
            }
            return refIndexDTO;
        }
        public List<IMPA_REF_INDEX> LoadRefIndexByL1(int l1Code)
        {
            DataTable dt = new DataTable();
            List<IMPA_REF_INDEX> lstIri = new List<IMPA_REF_INDEX>();
            //TAU tDTO = new TAU();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_REF_INDEXByL1Code";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@L1Code", l1Code);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstIri = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstIri;
        }
    }
}
